package org.ix.shiro.service.impl;

import org.ix.common.models.IxUser;
import org.ix.shiro.model.LoginModel;
import org.ix.shiro.service.AccountService;
import org.ix.shiro.service.JwtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AccountServiceImpl implements AccountService {

    final JwtService jwtService;
    private final Map<String, IxUser> userMap;

    @Autowired
    public AccountServiceImpl(JwtService jwtService) {
        this.jwtService = jwtService;
        this.userMap = getUserMap();
    }

    @Override
    public String login(LoginModel loginModel) {
        // 验证用户名密码
        return jwtService.createToken(loginModel.getAccount());
    }

    @Override
    public IxUser getUser(String username) {
        return userMap.getOrDefault(username, null);
    }

    @Override
    public Set<String> getUserRoles(Integer userId) {
        Set<String> roles = new HashSet<>();
        return roles;
    }

    @Override
    public Set<String> getUserPermissions(Collection<String> roles) {
        Set<String> permissions = new HashSet<>();
        permissions.add("*");
        return permissions;
    }

    public Map<String, IxUser> getUserMap() {
        Map<String, IxUser> map = new HashMap<>();
        IxUser user1 = new IxUser(1, "user1", "user1@ix.com", "13211111111", "user1", "");
        IxUser user2 = new IxUser(2, "user2", "user2@ix.com", "13222222222", "user2", "");
        IxUser user3 = new IxUser(3, "user3", "user3@ix.com", "13233333333", "user3", "");
        IxUser user4 = new IxUser(4, "user4", "user4@ix.com", "13244444444", "user4", "");
        IxUser user5 = new IxUser(5, "user5", "user5@ix.com", "13255555555", "user5", "");
        map.put("user1", user1);
        map.put("user2", user2);
        map.put("user3", user3);
        map.put("user4", user4);
        map.put("user5", user5);
        return map;
    }
}
